/**
 * QunTool 开启/关闭对话框时，SCRM 也同步开启/关闭
 */
import services from '@/services'

export function useSCRMSyncMask() {
  useEffect(() => {
    if (!services.scrm.inSCRM) return

    let prevHasMask = false
    const observer = new MutationObserver(() => {
      const hasMask =
        [...document.querySelectorAll('.ant-modal-mask')].filter(
          mask => !mask.className.includes('ant-fade-leave'),
        ).length > 0

      if (hasMask !== prevHasMask) {
        prevHasMask = hasMask
        services.scrm.send('mask-status', hasMask)
      }
    })
    observer.observe(document.body, { childList: true, subtree: true, attributes: true })
    return () => observer.disconnect()
  }, [])
}
